package com.go.where.manager.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.go.where.manager.dao.AccountRelationDao;
import com.go.where.manager.dao.CustomerDao;
import com.go.where.manager.page.BaseQuery.OrderType;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.pojo.AccountRelationInfo;
import com.go.where.manager.pojo.CustomerInfo;
import com.go.where.manager.service.CustomerService;
import com.go.where.manager.util.CommonUtil;

@Service("customerService")
public class CustomerServiceImpl implements CustomerService {

	@Resource(name = "customerDao")
	private CustomerDao customerDao;
	@Resource(name="accountRelationDao")
	private AccountRelationDao accountRelationDao;

	@Override
	public CustomerInfo queryByPrimartKey(String cid) {
		CustomerInfo info = customerDao.getByKey(cid);
		String customerId = info.getCustomerId();
		List<AccountRelationInfo> ariList = accountRelationDao.queryByCustomerId(customerId);
		info.setAccountList(ariList);
		int level = CommonUtil.getCustomerLevel(info.getCustomerScore());
		info.setCustomerLevel("VIP"+level);
		return info;
	}

	@Override
	public Pagination<CustomerInfo> queryPage(CustomerInfo customerInfo) {
		if (StringUtils.isNotEmpty(customerInfo.getOrderBy())
				&& customerInfo.getOrderType() != null) {
			if (customerInfo.getOrderType() == OrderType.asc) {
				customerInfo.setPage_orderByAsc("cu."
						+ customerInfo.getOrderBy());
			} else {
				customerInfo.setPage_orderByDesc("cu."
						+ customerInfo.getOrderBy());
			}
		}
		Integer count = customerDao.totalCount(customerInfo);
		customerInfo.calculate();
		List<CustomerInfo> list = customerDao.queryList(customerInfo);
		if(list!=null && !list.isEmpty()){
			for(CustomerInfo info : list){
				String customerId = info.getCustomerId();
				List<AccountRelationInfo> ariList = accountRelationDao.queryByCustomerId(customerId);
				info.setAccountList(ariList);
//				int level = CommonUtil.getCustomerLevel(info.getCustomerScore());
//				info.setCustomerLevel("VIP"+level);
			}
		}
		Pagination<CustomerInfo> page = customerDao.queryPagination(count,
				list, customerInfo);
		return page;
	}

	@Override
	public int update(CustomerInfo customerInfo) {
		return customerDao.update(customerInfo);
	}

	@Override
	public List<String> queryCustomerIds(CustomerInfo customerInfo) {
		return customerDao.queryCustomerIds(customerInfo);
	}

}
